package com.jjwxc.jwjskandriod.readActivity.local;

import com.jjwxc.jwjskandriod.readActivity.utils.Constant;
import com.jjwxc.jwjskandriod.readActivity.utils.ScreenUtils;
import com.jjwxc.jwjskandriod.readActivity.utils.SharedPreUtils;
import com.jjwxc.jwjskandriod.readActivity.view.PageMode;
import com.jjwxc.jwjskandriod.readActivity.view.PageStyle;


public class ReadSettingManager {

    /**
     * 默认选中状态
     */
    public static final int DEFAULT_READ_BG = 3;

    /**
     * 页面样式设置
     */
    public static final String SHARED_READ_BG = "shared_read_bg";

    /**
     * 亮度调节
     */
    public static final String SHARED_READ_BRIGHTNESS = "shared_read_brightness";

    /**
     * 字体大小本地存储 读书页设置面板
     */
    public static final String SHARED_READ_TEXT_SIZE = "shared_read_text_size";
    /**
     * 默认字体大小 读书页设置面板
     */
    public static final String SHARED_READ_IS_TEXT_DEFAULT = "shared_read_text_default";
    /**
     * 翻页动画配置 读书页设置面板
     */
    public static final String SHARED_READ_PAGE_MODE = "shared_read_mode";
    /**
     * 夜间模式 读书页设置面板
     */
    public static final String SHARED_READ_NIGHT_MODE = "shared_night_mode";

    /**
     * 音量键控制翻页 更多设置界面
     */
    public static final String SHARED_READ_VOLUME_TURN_PAGE = "shared_read_volume_turn_page";
    public static final String SHARED_READ_FULL_SCREEN = "shared_read_full_screen";
    /**
     * 是否转换为繁体字 更多设置界面
     */
    public static final String SHARED_READ_CONVERT_TYPE = "shared_read_convert_type";
    /**
     * 设置行间距 读书页设置面板
     */
    public static final String SHARED_READ_TEXT_INTERVAL = "shared_read_text_interval";
    /**
     * 设置字体编号
     */
    public static final String SHARED_READ_TEXT_TYPEFACE = "shared_read_text_typeface";
    public static final String SHARED_READ_TEXT_TYPEFACE_PATH = "shared_read_text_typeface_path";

    private static volatile ReadSettingManager sInstance;

    private SharedPreUtils sharedPreUtils;

    public static ReadSettingManager getInstance() {
        if (sInstance == null) {
            synchronized (ReadSettingManager.class) {
                if (sInstance == null) {
                    sInstance = new ReadSettingManager();
                }
            }
        }
        return sInstance;
    }

    private ReadSettingManager() {
        sharedPreUtils = SharedPreUtils.getInstance();
    }

    public void setTextInvterval(int invterval) {
        sharedPreUtils.putInt(SHARED_READ_TEXT_INTERVAL, invterval);
    }

    public int getTextInterval() {
        return sharedPreUtils.getInt(SHARED_READ_TEXT_INTERVAL, 1);
    }


    public void setPageStyle(PageStyle pageStyle) {
        sharedPreUtils.putInt(SHARED_READ_BG, pageStyle.ordinal());
    }

    public void setBrightness(int progress) {
        sharedPreUtils.putInt(SHARED_READ_BRIGHTNESS, progress);
    }

    public void setTypefacePath(int pos, String fileName) {
        sharedPreUtils.putInt(SHARED_READ_TEXT_TYPEFACE, pos);
        sharedPreUtils.putString(SHARED_READ_TEXT_TYPEFACE_PATH, fileName);
    }

    public String getTypefacePath() {
        int pos = sharedPreUtils.getInt(SHARED_READ_TEXT_TYPEFACE, 0);
        if (pos == 0) {
            return "";
        } else {
            return Constant.BOOK_CACHE_PATH + sharedPreUtils.getString(SHARED_READ_TEXT_TYPEFACE_PATH);
        }
    }

    public int getTypefacePos() {
        return sharedPreUtils.getInt(SHARED_READ_TEXT_TYPEFACE, 0);
    }

    public void setDefaultTextSize(boolean isDefault) {
        sharedPreUtils.putBoolean(SHARED_READ_IS_TEXT_DEFAULT, isDefault);
    }

    public void setTextSize(int textSize) {
        sharedPreUtils.putInt(SHARED_READ_TEXT_SIZE, textSize);
    }

    public void setPageMode(PageMode mode) {
        sharedPreUtils.putInt(SHARED_READ_PAGE_MODE, mode.ordinal());
    }

    public void setNightMode(boolean isNight) {
        sharedPreUtils.putBoolean(SHARED_READ_NIGHT_MODE, isNight);
    }

    public int getBrightness() {
        return sharedPreUtils.getInt(SHARED_READ_BRIGHTNESS, 40);
    }


    public int getTextSize() {
        return sharedPreUtils.getInt(SHARED_READ_TEXT_SIZE, ScreenUtils.spToPx(18));
    }

    public boolean isDefaultTextSize() {
        return sharedPreUtils.getBoolean(SHARED_READ_IS_TEXT_DEFAULT, false);
    }

    public PageMode getPageMode() {
        int mode = sharedPreUtils.getInt(SHARED_READ_PAGE_MODE, PageMode.SIMULATION.ordinal());
        return PageMode.values()[mode];
    }

    public PageStyle getPageStyle() {
        int style = sharedPreUtils.getInt(SHARED_READ_BG, PageStyle.BG_0.ordinal());
        return PageStyle.values()[style];
    }

    public int getPageStyleBgPos() {
        int style = sharedPreUtils.getInt(SHARED_READ_BG, PageStyle.BG_0.ordinal());
        return style;
    }

    public boolean isNightMode() {
        return sharedPreUtils.getBoolean(SHARED_READ_NIGHT_MODE, false);
    }

    public void setVolumeTurnPage(boolean isTurn) {
        sharedPreUtils.putBoolean(SHARED_READ_VOLUME_TURN_PAGE, isTurn);
    }

    public boolean isVolumeTurnPage() {
        return sharedPreUtils.getBoolean(SHARED_READ_VOLUME_TURN_PAGE, true);
    }

    public void setFullScreen(boolean isFullScreen) {
        sharedPreUtils.putBoolean(SHARED_READ_FULL_SCREEN, isFullScreen);
    }

    public boolean isFullScreen() {
        return sharedPreUtils.getBoolean(SHARED_READ_FULL_SCREEN, false);
    }

    public void setConvertType(int convertType) {
        sharedPreUtils.putInt(SHARED_READ_CONVERT_TYPE, convertType);
    }

    public int getConvertType() {
        return sharedPreUtils.getInt(SHARED_READ_CONVERT_TYPE, 0);
    }
}
